import { Component, OnInit,Output,Host,Inject,forwardRef,EventEmitter } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {DragdropService} from '../../services/dragdrop.service'

@Component({
  selector: 'app-keys',
  templateUrl: './keys.component.html',
  styleUrls: ['./keys.component.css']
})
export class KeysComponent implements OnInit {
  helpMessage={}
  curWidget:any;
  mesureGroup=[]
  dimensionGroup=[]
  draggedEl=false;
  draggedArray:any;      //保存被拖动的数组




  constructor(
    public translate:TranslateService,
    private dragdropservice:DragdropService
  ) { 
    translate.addLangs(['zh','en']);                          //添加语言支持
    translate.setDefaultLang('en');                           //设置默认语言
    let broswerLang = translate.getBrowserLang();              //获取浏览器支持的语言
    console.log(broswerLang)                                   //zh
    translate.use(broswerLang.match(/zh|en/) ? broswerLang : 'en');
  }

  @Output() keys=new EventEmitter<any>()
  ngOnInit() {
  }

  findIndex(el:any) {
    let index = -1;
    for(let i = 0; i < this.dimensionGroup.length; i++) {
        if(el.vin === this.dimensionGroup[i].vin) {
            index = i;
            break;
        }
    }
    return index;
  }
  keydroped(el){
      let t=this.dragdropservice.getdrag()
      let tcol = this.dragdropservice.getdragcol();
      console.log(t)
      console.log(t.a._events[0])        //car_type 拖动的名字
      console.log(t.b._events[0])        //type值
      console.log(t.c._events)         //数组[]
      if(t.a._events[0]&&t.b._events[0]=='level'){
        t.a._events[0]['other']='a';
        console.log(t.a._events[0])
        this.dimensionGroup = [...this.dimensionGroup,t.a._events[0]];
        console.log(this.dimensionGroup)
        let draggedCarIndex = this.findIndex(t.a._events[0]);
        t.c._events[0] = t.c._events[0].filter((val,i) => i!=draggedCarIndex);
        t.a._events[0]= null;
      }
       
      console.log(this.dimensionGroup)
      //接收dimension.column
      if(tcol.a._events[0]&&tcol.b._events[0]=='column'){
        tcol.a._events[0]['other']='b';
        console.log(tcol.a._events[0])
        this.dimensionGroup = [...this.dimensionGroup,tcol.a._events[0]]
        console.log(this.dimensionGroup)
        let draggedCarIndex = this.findIndex(tcol.a._events[0]);
        tcol.c._events[0] = tcol.c._events[0].filter((val,i) => i!=draggedCarIndex);
        tcol.a._events[0]= null;
      }
      this.keys.emit(this.dimensionGroup)
  }

  emitColumn(el,ev){
      console.log(el)
      //点击删除时调用backColumn
      this.dragdropservice.dropEl.emit(el)
  }

}
